package com.haixiaoke.smartLock.yaluete;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.haixiaoke.common.exception.ServiceException;
import com.haixiaoke.common.utils.RandomNoUtils;
import com.haixiaoke.common.utils.StringUtils;
import com.haixiaoke.common.utils.sign.AesUtils;
import com.haixiaoke.domain.*;
import com.haixiaoke.service.*;
import com.haixiaoke.vo.YltAuthVo;
import com.haixiaoke.vo.YltDataVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 亚略特智能锁接口
 *
 * @author qixi
 * 2023-06-01
 */
@Service
@Slf4j
public class SmartLockYLTEventService {

    @Autowired
    private ILockYltAccreditService lockYltAccreditService;

    @Autowired
    private SmartLockYLTService smartLockYLTService;
    @Autowired
    private ISmartLockPwdService smartLockPwdService;
    @Autowired
    private ISmartLockFingerprintService smartLockFingerprintService;
    @Autowired
    private ISmartLockCardService smartLockCardService;
    @Autowired
    private ISmartLockBluetoothService smartLockBluetoothService;
    @Autowired
    private ILockUnlockLogService lockUnlockLogService;
    @Autowired
    private ILockOfflineLogService lockOfflineLogService;
    @Autowired
    private ILockInfoLogService lockInfoLogService;
    @Autowired
    private ILockWarningLogService lockWarningLogService;
    @Autowired
    private ISmartLockService smartLockService;


    // 锁绑定商户
    public int insertSmartLock(SmartLock smartLock) {
        String sn = smartLock.getSn();
        if (StrUtil.isEmpty(sn)) {
            throw new ServiceException("参数有误");
        }

        if (!smartLockService.checkLockSnUnique(sn)) {
            throw new ServiceException("该锁已绑定");
        }

        String result = smartLockYLTService.lockBindingMerchants(smartLock);
        JSONObject jsonObject = JSONObject.parseObject(result);
        String code = jsonObject.get("code").toString();
        if ("0".equals(code)) {
            return smartLockService.insertSmartLock(smartLock);
        } else {
            return 0;
        }
    }


    /**
     * 新增智能锁指纹授权信息和授权指纹
     *
     * @param smartLockFingerprint 智能锁指纹授权信息
     * @return 结果
     */
    public int insertSmartLockFingerprint(SmartLockFingerprint smartLockFingerprint) {

        String fingerprint = smartLockFingerprint.getFingerprint();
        String identification = smartLockFingerprint.getIdentification();
        String sn = smartLockFingerprint.getSn();
        Date startTime = smartLockFingerprint.getStartTime();
        Date endTime = smartLockFingerprint.getEndTime();
        if (StrUtil.isEmpty(fingerprint) || StrUtil.isEmpty(identification) || StrUtil.isEmpty(sn)) {
            throw new ServiceException("参数有误");
        }

        String result = "";
        SmartLock smartLock = new SmartLock();
        YltAuthVo yltAuthVo = lockYltAccreditService.getYLTAuthInfoBySNAndIdentification(sn, identification);
        if (ObjectUtil.isNotNull(yltAuthVo)) {
            Integer fOpenByFinger = yltAuthVo.getFOpenByFinger();
            if (fOpenByFinger.equals(LockYltAccredit.AUTH_STATE_TRUE)) {
                throw new ServiceException("该用户指纹已授权");
            }

            Date fBeginDate = yltAuthVo.getFBeginDate();
            Date fEndDate = yltAuthVo.getFEndDate();
            if (!startTime.equals(fBeginDate) || !endTime.equals(fEndDate)) {
                throw new ServiceException("授权时间与用户授权时间不一致");
            }
            smartLock.setSn(sn);
            smartLock.setIdentification(identification);
            smartLock.setFingerprint(smartLockFingerprint.getFingerprint());
            smartLock.setYltAuthVo(yltAuthVo);
            result = smartLockYLTService.addFingerprint(smartLock);
        } else {
            if (ObjectUtil.isNull(startTime) || ObjectUtil.isNull(endTime)) {
                throw new ServiceException("授权时间有误!");
            }
            smartLock.setSn(sn);
            smartLock.setIdentification(identification);
            smartLock.setStartTime(startTime);
            smartLock.setEndTime(endTime);
            smartLock.setFingerprint(smartLockFingerprint.getFingerprint());

            // 构建参数
            yltAuthVo = YltAuthVo.createDefault();
            yltAuthVo.setFBeginDate(startTime);
            yltAuthVo.setFEndDate(endTime);
            yltAuthVo.setFAppUserID(identification);
            yltAuthVo.setFLockMAC(sn);
            smartLock.setYltAuthVo(yltAuthVo);
            result = smartLockYLTService.addFingerprint(smartLock);
        }
        if (StrUtil.isEmpty(result)) {
            return 0;
        }

        JSONObject jsonObject = JSONObject.parseObject(result);
        String code = jsonObject.get("code").toString();
        if ("0".equals(code)) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 刪除指纹授权
     *
     * @param smartLockFingerprint 指纹授权对象
     * @return
     */
    public int deleteSmartLockFingerprint(SmartLockFingerprint smartLockFingerprint) {

        String identification = smartLockFingerprint.getIdentification();
        String sn = smartLockFingerprint.getSn();
        if (StrUtil.isEmpty(identification) || StrUtil.isEmpty(sn)) {
            throw new ServiceException("参数有误");
        }
        SmartLock smartLock = new SmartLock();
        YltAuthVo yltAuthVo = lockYltAccreditService.getYLTAuthInfoBySNAndIdentification(sn, identification);
        smartLock.setSn(sn);
        smartLock.setIdentification(identification);
        smartLock.setFingerprint(smartLockFingerprint.getFingerprint());
        smartLock.setYltAuthVo(yltAuthVo);
        String result = smartLockYLTService.addFingerprint(smartLock);

        JSONObject jsonObject = JSONObject.parseObject(result);
        String code = jsonObject.get("code").toString();
        if ("0".equals(code)) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 新增密码授权
     *
     * @param smartLockPwd 密码授权对象
     * @return
     */
    public int insertSmartLockPwd(SmartLockPwd smartLockPwd) {

        String password = smartLockPwd.getPassword();

        if (SmartLockPwd.PASSWORD_GENERATE_TYPE_SYSGEN.equals(smartLockPwd.getGenerateType())) {
            password = RandomUtil.randomNumbers(6);
        } else {
            if (StrUtil.isEmpty(password)) {
                throw new ServiceException("密码不能为空");
            }
            if (password.length() != 6 || !StringUtils.isNumeric(smartLockPwd.getPassword())) {
                throw new ServiceException("请输入6位数字密码");
            }
        }
        String identification = smartLockPwd.getIdentification();
        String sn = smartLockPwd.getSn();
        if (StrUtil.isEmpty(identification) || StrUtil.isEmpty(sn)) {
            throw new ServiceException("参数有误");
        }
        Date startTime = smartLockPwd.getStartTime();
        Date endTime = smartLockPwd.getEndTime();

        String result = "";
        SmartLock smartLock = new SmartLock();
        YltAuthVo yltAuthVo = lockYltAccreditService.getYLTAuthInfoBySNAndIdentification(sn, identification);
        if (ObjectUtil.isNotNull(yltAuthVo)) {
            Integer fOpenPassword = yltAuthVo.getFOpenByPassword();
            if (fOpenPassword.equals(LockYltAccredit.AUTH_STATE_TRUE)) {
                throw new ServiceException("用户密码已授权");
            }

            Date fBeginDate = yltAuthVo.getFBeginDate();
            Date fEndDate = yltAuthVo.getFEndDate();
            if (!startTime.equals(fBeginDate) || !endTime.equals(fEndDate)) {
                throw new ServiceException("授权时间与用户授权时间不一致");
            }
            smartLock.setSn(sn);
            smartLock.setIdentification(identification);
            smartLock.setPassword(password);
            smartLock.setYltAuthVo(yltAuthVo);
            result = smartLockYLTService.addPassword(smartLock);
        } else {
            if (ObjectUtil.isNull(startTime) || ObjectUtil.isNull(endTime)) {
                throw new ServiceException("授权时间有误!");
            }
            smartLock.setSn(sn);
            smartLock.setIdentification(identification);
            smartLock.setStartTime(startTime);
            smartLock.setEndTime(endTime);
            smartLock.setPassword(password);

            // 构建参数
            yltAuthVo = YltAuthVo.createDefault();
            yltAuthVo.setFBeginDate(startTime);
            yltAuthVo.setFEndDate(endTime);
            yltAuthVo.setFAppUserID(identification);
            yltAuthVo.setFLockMAC(sn);
            smartLock.setYltAuthVo(yltAuthVo);
            result = smartLockYLTService.addPassword(smartLock);
        }
        if (StrUtil.isEmpty(result)) {
            return 0;
        }

        JSONObject jsonObject = JSONObject.parseObject(result);
        String code = jsonObject.get("code").toString();
        if ("0".equals(code)) {
            return 1;
        } else {
            return 0;
        }

    }

    /**
     * 删除密码
     *
     * @param sn             智能锁sn
     * @param identification 用户标识
     * @return
     */
    public int deleteSmartLockPwd(String sn, String identification) {
        if (StrUtil.isEmpty(identification) || StrUtil.isEmpty(sn)) {
            throw new ServiceException("参数有误");
        }
        SmartLock smartLock = new SmartLock();
        YltAuthVo yltAuthVo = lockYltAccreditService.getYLTAuthInfoBySNAndIdentification(sn, identification);
        smartLock.setSn(sn);
        smartLock.setIdentification(identification);
        smartLock.setYltAuthVo(yltAuthVo);
        String result = smartLockYLTService.deletePwd(smartLock);
        if (StrUtil.isEmpty(result)) {
            return 0;
        }

        JSONObject jsonObject = JSONObject.parseObject(result);
        String code = jsonObject.get("code").toString();
        if ("0".equals(code)) {
            return 1;
        } else {
            return 0;
        }
    }

    public int insertSmartLockCard(SmartLockCard card) {
        String cardNumber = card.getCardNumber();
        String identification = card.getIdentification();
        String sn = card.getSn();
        Integer cardType = card.getCardType();
        if (StrUtil.isEmpty(identification) || StrUtil.isEmpty(sn) ||
                StrUtil.isEmpty(cardNumber) || ObjectUtil.isNull(cardType)) {
            throw new ServiceException("参数有误");
        }
        Date startTime = card.getStartTime();
        Date endTime = card.getEndTime();

        String result = "";
        SmartLock smartLock = new SmartLock();
        YltAuthVo yltAuthVo = lockYltAccreditService.getYLTAuthInfoBySNAndIdentification(sn, identification);
        if (ObjectUtil.isNotNull(yltAuthVo)) {
            if (SmartLockCard.IDENTITY_CARD_TYPE.equals(cardType)) {
                Integer fOpenByIDCard = yltAuthVo.getFOpenByIDCard();
                if (fOpenByIDCard.equals(LockYltAccredit.AUTH_STATE_TRUE)) {
                    throw new ServiceException("用户身份证已授权");
                }
            } else {
                Integer fOpenByICCard = yltAuthVo.getFOpenByICCard();
                if (fOpenByICCard.equals(LockYltAccredit.AUTH_STATE_TRUE)) {
                    throw new ServiceException("用户ic卡已授权");
                }
            }

            Date fBeginDate = yltAuthVo.getFBeginDate();
            Date fEndDate = yltAuthVo.getFEndDate();
            if (!startTime.equals(fBeginDate) || !endTime.equals(fEndDate)) {
                throw new ServiceException("授权时间与用户授权时间不一致");
            }
            smartLock.setSn(sn);
            smartLock.setIdentification(identification);
            smartLock.setCardNumber(cardNumber);
            smartLock.setYltAuthVo(yltAuthVo);
            if (SmartLockCard.IDENTITY_CARD_TYPE.equals(cardType)) {
                result = smartLockYLTService.addIdCard(smartLock);
            } else {
                result = smartLockYLTService.addCard(smartLock);
            }
        } else {
            if (ObjectUtil.isNull(startTime) || ObjectUtil.isNull(endTime)) {
                throw new ServiceException("授权时间有误!");
            }
            smartLock.setSn(sn);
            smartLock.setIdentification(identification);
            smartLock.setStartTime(startTime);
            smartLock.setEndTime(endTime);
            smartLock.setCardNumber(cardNumber);

            // 构建参数
            yltAuthVo = YltAuthVo.createDefault();
            yltAuthVo.setFBeginDate(startTime);
            yltAuthVo.setFEndDate(endTime);
            yltAuthVo.setFAppUserID(identification);
            yltAuthVo.setFLockMAC(sn);
            smartLock.setYltAuthVo(yltAuthVo);
            if (SmartLockCard.IDENTITY_CARD_TYPE.equals(cardType)) {
                result = smartLockYLTService.addIdCard(smartLock);
            } else {
                result = smartLockYLTService.addCard(smartLock);
            }
        }
        if (StrUtil.isEmpty(result)) {
            return 0;
        }

        JSONObject jsonObject = JSONObject.parseObject(result);
        String code = jsonObject.get("code").toString();
        if ("0".equals(code)) {
            return 1;
        } else {
            return 0;
        }
    }

    public int deleteSmartLockCard(SmartLockCard card) {
        String identification = card.getIdentification();
        String sn = card.getSn();
        Integer cardType = card.getCardType();

        if (StrUtil.isEmpty(identification) || StrUtil.isEmpty(sn) || ObjectUtil.isNull(cardType)) {
            throw new ServiceException("参数有误");
        }
        String result = "";
        SmartLock smartLock = new SmartLock();
        YltAuthVo yltAuthVo = lockYltAccreditService.getYLTAuthInfoBySNAndIdentification(sn, identification);
        smartLock.setSn(sn);
        smartLock.setIdentification(identification);
        smartLock.setYltAuthVo(yltAuthVo);
        if (SmartLockCard.IDENTITY_CARD_TYPE.equals(cardType)) {
            result = smartLockYLTService.deleteIdCard(smartLock);
        } else {
            result = smartLockYLTService.deleteCard(smartLock);
        }
        if (StrUtil.isEmpty(result)) {
            return 0;
        }

        JSONObject jsonObject = JSONObject.parseObject(result);
        String code = jsonObject.get("code").toString();
        if ("0".equals(code)) {
            return 1;
        } else {
            return 0;
        }
    }


    public int insertSmartLockBluetooth(SmartLockBluetooth smartLockBluetooth) {

        String identification = smartLockBluetooth.getIdentification();
        String sn = smartLockBluetooth.getSn();
        if (StrUtil.isEmpty(identification) || StrUtil.isEmpty(sn)) {
            throw new ServiceException("参数有误");
        }
        Date startTime = smartLockBluetooth.getStartTime();
        Date endTime = smartLockBluetooth.getEndTime();

        String result = "";
        SmartLock smartLock = new SmartLock();
        YltAuthVo yltAuthVo = lockYltAccreditService.getYLTAuthInfoBySNAndIdentification(sn, identification);
        if (ObjectUtil.isNotNull(yltAuthVo)) {
            Integer fOpenByBLE = yltAuthVo.getFOpenByBLE();
            if (fOpenByBLE.equals(LockYltAccredit.AUTH_STATE_TRUE)) {
                throw new ServiceException("用户蓝牙已授权");
            }
            Date fBeginDate = yltAuthVo.getFBeginDate();
            Date fEndDate = yltAuthVo.getFEndDate();
            if (!startTime.equals(fBeginDate) || !endTime.equals(fEndDate)) {
                throw new ServiceException("授权时间与用户授权时间不一致");
            }
            smartLock.setSn(sn);
            smartLock.setIdentification(identification);
            smartLock.setYltAuthVo(yltAuthVo);
            result = smartLockYLTService.addBluetooth(smartLock);
        } else {
            if (ObjectUtil.isNull(startTime) || ObjectUtil.isNull(endTime)) {
                throw new ServiceException("授权时间有误!");
            }
            smartLock.setSn(sn);
            smartLock.setIdentification(identification);
            smartLock.setStartTime(startTime);
            smartLock.setEndTime(endTime);

            // 构建参数
            yltAuthVo = YltAuthVo.createDefault();
            yltAuthVo.setFBeginDate(startTime);
            yltAuthVo.setFEndDate(endTime);
            yltAuthVo.setFAppUserID(identification);
            yltAuthVo.setFLockMAC(sn);
            smartLock.setYltAuthVo(yltAuthVo);
            result = smartLockYLTService.addBluetooth(smartLock);
        }
        if (StrUtil.isEmpty(result)) {
            return 0;
        }

        JSONObject jsonObject = JSONObject.parseObject(result);
        String code = jsonObject.get("code").toString();
        if ("0".equals(code)) {
            return 1;
        } else {
            return 0;
        }

    }

    public int deleteSmartLockBluetooth(SmartLockBluetooth smartLockBluetooth) {

        String identification = smartLockBluetooth.getIdentification();
        String sn = smartLockBluetooth.getSn();
        if (StrUtil.isEmpty(identification) || StrUtil.isEmpty(sn)) {
            throw new ServiceException("参数有误");
        }

        SmartLock smartLock = new SmartLock();
        YltAuthVo yltAuthVo = lockYltAccreditService.getYLTAuthInfoBySNAndIdentification(sn, identification);
        smartLock.setSn(sn);
        smartLock.setIdentification(identification);
        smartLock.setYltAuthVo(yltAuthVo);
        String result = smartLockYLTService.deleteBle(smartLock);
        if (StrUtil.isEmpty(result)) {
            return 0;
        }

        JSONObject jsonObject = JSONObject.parseObject(result);
        String code = jsonObject.get("code").toString();
        if ("0".equals(code)) {
            return 1;
        } else {
            return 0;
        }

    }

    public List<SmartLockDetails> selectLockCard(String sn) {
        List<SmartLockDetails> list = new ArrayList<>();
        List<SmartLockCard> smartLockCards = smartLockCardService.selectSmartLockCardBySN(sn);

        for (SmartLockCard smartLockCard : smartLockCards) {
            SmartLockDetails details = new SmartLockDetails();
            details.setCardType(smartLockCard.getCardType());
            details.setIdentification(smartLockCard.getIdentification());
            details.setStartTime(DateUtil.format(smartLockCard.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
            details.setEndTime(DateUtil.format(smartLockCard.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
            details.setCreateAt(DateUtil.format(smartLockCard.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
            list.add(details);
        }
        return list;
    }

    public List<SmartLockDetails> getLockPassword(String sn) {
        List<SmartLockDetails> list = new ArrayList<>();
        List<SmartLockPwd> smartLockPwds = smartLockPwdService.selectSmartLockPwdBySN(sn);

        for (SmartLockPwd pwd : smartLockPwds) {
            SmartLockDetails details = new SmartLockDetails();
            details.setPassword(pwd.getPassword());
            details.setIdentification(pwd.getIdentification());
            details.setStartTime(DateUtil.format(pwd.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
            details.setEndTime(DateUtil.format(pwd.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
            details.setCreateAt(DateUtil.format(pwd.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
            list.add(details);
        }
        return list;
    }

    public List<SmartLockDetails> getBluetooth(String sn) {

        List<SmartLockDetails> list = new ArrayList<>();
        List<SmartLockBluetooth> bluetooths = smartLockBluetoothService.selectSmartLockBluetoothBySN(sn);

        for (SmartLockBluetooth bluetooth : bluetooths) {
            SmartLockDetails details = new SmartLockDetails();
            details.setIdentification(bluetooth.getIdentification());
            details.setStartTime(DateUtil.format(bluetooth.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
            details.setEndTime(DateUtil.format(bluetooth.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
            details.setCreateAt(DateUtil.format(bluetooth.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
            list.add(details);
        }
        return list;
    }

    public List<YltAuthVo> getYltAuth(String sn) {
        List<YltAuthVo> yltAuthInfoList = lockYltAccreditService.getYLTAuthInfoList(sn);
        return yltAuthInfoList;
    }

    /**
     * 开锁日志
     *
     * @param list
     */
    public void smartLockUnlockLog(List<Map<String, Object>> list) {
        for (Map<String, Object> map : list) {
            Date date = new Date();
            // map转对象
            YltDataVO yltDataVO = BeanUtil.fillBeanWithMap(map, new YltDataVO(), false);

            LockUnlockLog log = new LockUnlockLog();
            log.setLockSn(yltDataVO.getFLockMAC());
            Integer fOpenWay = yltDataVO.getFOpenWay();
            // 将开锁状态转换为系统开锁类型
            log.setUnlockType(YLTUnlockTypeConvert(fOpenWay));
            log.setUnlockState(0);
            log.setUnlockTime(yltDataVO.getFStorageTime());

            String fUserID = yltDataVO.getFUserID();
            String id = null;
            try {
                id = AesUtils.aesDecrypt(fUserID, yltDataVO.getFLockMAC());
            } catch (Exception e) {
                throw new ServiceException("aes解密异常!");
            }


            log.setIdentification(id);
            log.setBrandId(LockWarningLog.YLT_BRAND_ID);
            log.setBrandName(LockWarningLog.YLT_BRAND_NAME);
            log.setModelId(LockWarningLog.YLT_BRAND_MODEL_ID);
            log.setModelName(LockWarningLog.YLT_BRAND_MODEL_NAME);
            log.setCreateTime(date);
            lockUnlockLogService.insertLockUnlockLog(log);
        }

    }


    /**
     * 心跳日志
     *
     * @param list
     */
    public void smartLockInfoLog(List<Map<String, Object>> list) {
        for (Map<String, Object> map : list) {
            Date date = new Date();
            // map转对象
            YltDataVO yltDataVO = BeanUtil.fillBeanWithMap(map, new YltDataVO(), false);
            LockInfoLog log = new LockInfoLog();
            Integer fBatVol = yltDataVO.getFBatVol();
            log.setBattery(YLTVoltageConvertBatteryInfo(fBatVol));
            log.setInfoType(0);
            log.setLockSn(yltDataVO.getFLockMAC());
            log.setCreateTime(date);
            log.setBrandId(LockWarningLog.YLT_BRAND_ID);
            log.setBrandName(LockWarningLog.YLT_BRAND_NAME);
            log.setModelId(LockWarningLog.YLT_BRAND_MODEL_ID);
            log.setModelName(LockWarningLog.YLT_BRAND_MODEL_NAME);
            lockInfoLogService.insertLockInfoLog(log);
        }
    }

    /**
     * 告警日志
     *
     * @param list
     */
    public void smartLockWarningLog(List<Map<String, Object>> list) {
        Date date = new Date();

        for (Map<String, Object> map : list) {

            YltDataVO yltDataVO = BeanUtil.fillBeanWithMap(map, new YltDataVO(), false);

            LockWarningLog log = new LockWarningLog();
            log.setLockSn(yltDataVO.getFLockMAC());

            Integer warningType = YLTWarningTypeConvert(yltDataVO.getFEvent());
            if (ObjectUtil.isNull(warningType)) {
                continue;
            }
            log.setWarningType(warningType);
            log.setCreateTime(yltDataVO.getFActiveTime());
            log.setBrandId(LockWarningLog.YLT_BRAND_ID);
            log.setBrandName(LockWarningLog.YLT_BRAND_NAME);
            log.setModelId(LockWarningLog.YLT_BRAND_MODEL_ID);
            log.setModelName(LockWarningLog.YLT_BRAND_MODEL_NAME);
            lockWarningLogService.insertLockWarningLog(log);
        }
    }

    /**
     * 删除智能锁并删除所有授权并解绑商户
     *
     * @param smartLock
     * @return
     */
    public Integer removeSmartLock(SmartLock smartLock) {
        String sn = smartLock.getSn();
        String result = smartLockYLTService.untieLockAuth(sn);
        JSONObject jsonObject = JSONObject.parseObject(result);
        String code = jsonObject.get("code").toString();
        if (!"0".equals(code)) {
            String msg = jsonObject.get("message").toString();
            log.error("亚略特解绑授权异常 msg: -{}", msg);
            throw new ServiceException(" 亚略特解绑授权异常! ");
        }

        return smartLockService.deleteSmartLockByLockId(smartLock.getLockId());
    }

    private Integer YLTUnlockTypeConvert(Integer fOpenWay) {

        // 亚略特开锁方式 1指紋 2密碼 4身份证/卡 8蓝牙 16人脸 32虹膜 64钥匙 124内开 125密码池密码 126 mifare卡
        /**
         * 系统开锁类型:1：永久密码 2：指纹 3：临时密码 4：远程开锁 5：蓝牙开锁 6：cookie开锁 7：门卡开锁
         * 8- 撤防/布防 10：机械钥匙开门 11：反锁 12 - 门内开锁 13:装修密码 14:装修密码开锁失败 15:离线开锁
         */
        Integer type = null;
        switch (fOpenWay) {
            case 1:
                type = 2;
                break;
            case 2:
                type = 1;
                break;
            case 4:
                type = 7;
                break;
            case 8:
                type = 5;
                break;
            case 64:
                type = 10;
                break;
            case 124:
                type = 12;
                break;
        }
        return type;
    }

    private Integer YLTWarningTypeConvert(Integer warningType) {

        /**
         * 亚略特事件 0重启 1低电量 2设备被拆除 4陌生人尝试开锁 8高电压 16上电/重启 32硬件错误 64门未关 65门已关 66钥匙未拔
         * 67钥匙拔出 68反锁关闭 69反锁开启 80触发同步 81解码成功 82解码失败 83 添加指纹 84删除指纹 128胁迫报警
         */
        /** '告警类型: 1：撞击报警 2：低电量 3：撬锁报警 5：长时间未关
         * 6：蓝牙cookie错误 8：非法开门报警 9：密码错误自锁报警'*/
        Integer type = null;
        switch (warningType) {
            case 1:
                type = 2;
                break;
            case 2:
                type = 3;
                break;
            case 4:
                type = 9;
                break;
            case 64:
                type = 5;
                break;
            case 128:
                type = 8;
                break;
        }
        return type;
    }


    public static Integer YLTVoltageConvertBatteryInfo(Integer voltage) {
        BigDecimal bigDecimal = BigDecimal.valueOf(voltage).divide(new BigDecimal("10"));
        // 亚略特电压转电量方式 X - 4.6 / 1.4 * 100
        Integer battery = null;

        if (bigDecimal.compareTo(new BigDecimal("6")) > -1) {
            battery = 100;
        } else if (bigDecimal.compareTo(new BigDecimal("4.4")) < 1) {
            battery = 0;
        } else {
            battery = bigDecimal.subtract(new BigDecimal("4.6")).divide(new BigDecimal("1.4"), 2, RoundingMode.HALF_UP)
                    .multiply(new BigDecimal(100)).intValue();
        }
        return battery;
    }


}
